#!/bin/bash
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
source ${baseDirForScriptSelf}/source.sh
unset http_proxy
unset https_proxy
is_huawei_tmp=`curl -I -m 10 -o /dev/null -s -w %{http_code} w3.huawei.com`
if [ "${is_huawei_tmp}" == "200" ];then
    is_huawei=true
else
    is_huawei=false
fi
## 内网不需要配置代理
if [ "${is_huawei}" == "true" ];then
    unset http_proxy;unset https_proxy
else
    source ${baseDirForScriptSelf}/proxy.sh
fi
# 准备python3,20.04默认是3.8.x
os_type=`lsb_release  -i|awk '{print $NF}'|tr '[A-Z]' '[a-z]'`
if [ "${os_type}" == 'ubuntu' ];then
    sudo bash -c "apt-get install -y python3.8 >>${baseDirForScriptSelf}/init_run.log 2>&1"
else
    echo "${os_type} 系统需要源码编译python3.8"
    unset http_proxy
    unset https_proxy
    sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev curl

    cd ${work_dir}
    source ${baseDirForScriptSelf}/proxy.sh
    curl -s -k -L --retry 5 "https://repo.huaweicloud.com/python/3.8.0/Python-3.8.0.tar.xz" --output "Python-3.8.0.tar.xz"
    sudo tar Jxvf Python-3.8.0.tar.xz
    cd Python-3.8.0
    sudo ./configure #--enable-optimizations
    sudo make 
    sudo make install
    sudo ln -s /usr/share/pyshared/lsb_release.py /usr/local/lib/python3.8/site-packages/lsb_release.py
fi


python_file=`which python 2>&1|grep -v which`
python38_file=`which python3.8 2>&1|grep -v which`
sudo bash -c "update-alternatives --install ${python_file} python ${python38_file} 1 >>${baseDirForScriptSelf}/init_run.log 2>&1"
sudo bash -c "update-alternatives --install ${python_file}3 python3 ${python38_file} 1 >>${baseDirForScriptSelf}/init_run.log 2>&1"
sudo bash -c "apt-get install -y python3-setuptools python3-pip -y >>${baseDirForScriptSelf}/init_run.log 2>&1"
python3.8 -m pip install --upgrade pip >>${baseDirForScriptSelf}/init_run.log 2>&1
python3.8 -m pip install ecdsa Crypto pycryptodome pycryptodomex >>${baseDirForScriptSelf}/init_run.log 2>&1
# 解决ModuleNotFoundError: No module named 'apt_pkg'
sudo bash -c " apt install -y python3-apt >>${baseDirForScriptSelf}/init_run.log 2>&1"
cd /usr/lib/python3/dist-packages/
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so
# 判断python版本是否满足3.7+要求
python_version=`python3.8 --version|awk '{print $2}'|sed 's/\.//g'`
if [ ${python_version//./} -ge 370 ];then
    echo -e "${success_color},Python version ${python_version}"
else
    echo -e "${failed_color},Python version Check Failed"
    exit 1
fi
source /etc/profile
